package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetMgr;
import com.yanqu.road.server.pb.banquet.CrossBanquetPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;


@Cmd(code = Protocol.C_CROSS_BANQUET_USER_REWARD, desc = "获得跨服私人宴会奖励")
public class GetBanquetUserRewardCmd extends CrossCommand {


    private int beautiesSkillAdd;
    private boolean autoOpen;
    private String banquetId = "";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossBanquetProto.CrossBanquetGetRewardReq reqMsg = CrossBanquetProto.CrossBanquetGetRewardReq.parseFrom(packet.getBytes());
        beautiesSkillAdd = reqMsg.getBeautiesSkillAdd();
        if(reqMsg.hasAutoOpen()){
            autoOpen = reqMsg.getAutoOpen();
        }
        if(reqMsg.hasBanquetId()){
            banquetId = reqMsg.getBanquetId();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        Map<String,CrossBanquetData> banquetMap = CrossBanquetMgr.getOverBanquetUserReward(packet.getPlayerId(),beautiesSkillAdd);
        CrossBanquetProto.CrossBanquetGetRewardResp.Builder respMsg = CrossBanquetProto.CrossBanquetGetRewardResp.newBuilder();
        respMsg.setAutoOpen(autoOpen);
        for(CrossBanquetData banquet :banquetMap.values()){
            BanquetProto.BanquetGetRewardTempMsg.Builder temp = BanquetProto.BanquetGetRewardTempMsg.newBuilder();
            temp.setBanquetId(banquet.getBanquetId());
            temp.setType(banquet.getType());
            temp.setOpenTime(banquet.getOpenTime());
            temp.setInfo(banquet.getInvitationInfo());
            temp.setPopularity(banquet.getPopularity());
            temp.setIntegral(banquet.getIntegral());
            if(banquet.getMemberMap() != null){
                for(CrossBanquetMember member : banquet.getMemberMap().values()){
                    temp.addMemberRewardList(CrossBanquetPb.parseMemberRewardTempMsg(member));
                }
            }
            temp.setReward(banquet.getEndReward());
            temp.setMemCount(banquet.getUserCount());
            respMsg.addRewardList(temp);
        }
        respMsg.setRet(0);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_BANQUET_CROSS_USER_REWARD_RESP, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
